//
//  MBDUserInfoViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/10/23.
//

import Foundation

import LGAlertView
import UIKit
import YYText
import Toast_Swift
import Alamofire
import SwiftyJSON
import RxCocoa
import RxSwift
import Kingfisher

class MBDUserInfoViewController : UIViewController {
    
    var tableView: UITableView!
    var viewModel : MBDUserInfoViewModel!
    var disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "个人信息"
        //如果 viewmodel 为空再赋值
        if viewModel == nil {
            fatalError("MBDUserInfoViewModel not implement")
        }
        
        let rightItem = UIButton(frame: CGRectMake(0, 0, 45, 25))
        rightItem.setImage(UIImage(named: "ico_more"), for: .normal)
        rightItem.addTarget(self, action: #selector(rightItemPressed), for: .touchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightItem)
        
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
        self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
                    self.view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
                    self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
                    self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
                ])
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 100

        tableView.register(MBDUserDetailCell.self, forCellReuseIdentifier: "MBDUserDetailCell")
        
        self.viewModel.cellModels = MBDUserInfoViewModel.defaultCellModels(realName : self.viewModel.realName ?? "无名氏", avatar :self.viewModel.avatarUrl ?? MBDPath2URLUtils.filePathPrefix.appending("AppIcon"))
        
        self.tableView.reloadData()
        
    }
    
    @objc func rightItemPressed(){
        // 设置源为相机
        let alert = LGAlertView(title: nil, message: nil, style: .actionSheet, buttonTitles: ["投诉","拉黑"], cancelButtonTitle: "取消", destructiveButtonTitle: nil, actionHandler: {alert,index, title in
            if index == 0 {
                let reportViewModel = MBDUserReportViewModel()
                let reportViewController = MBDUserReportViewController(viewModel: reportViewModel)
                self.navigationController?.pushViewController(reportViewController, animated: true)
            } else if 1 == index {
                if let blacklistUserId = self.viewModel.userId {
                    self.viewModel.doRequest(blacklistUserId: blacklistUserId) { error, result in
                        if error == nil {
                            self.view.makeToast("拉黑成功，您将看不到该用户发布的群组",duration: 2,position: .center)
                        } else {
                            self.view.makeToast(error!.localizedDescription,duration: 2,position: .center)

                        }
                    }
                } else {
                    self.view.makeToast("出了点问题，请联系开发小哥哥",duration: 2,position: .center)
                }
                
            }

        }, cancelHandler: { alertView in
            
        })
        alert.buttonsHeight = 40
        alert.cancelButtonOffsetY = 40
        alert.show()
        
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    override var prefersStatusBarHidden: Bool {
        return false
    }
    
}


extension MBDUserInfoViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.viewModel.cellModels?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellModel = self.viewModel.cellModels![indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "MBDUserDetailCell", for: indexPath)
        let realCell = cell as! MBDUserDetailCell
        realCell.cellModel = cellModel
        
        return cell
    }
}


extension MBDUserInfoViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cellModel = self.viewModel.cellModels![indexPath.row]
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cellModel = self.viewModel.cellModels![indexPath.row] as MBDUserDetailCellModel

        return cellModel.cellHeight()
    }
}
